/**
 * Created by Alex Tan on 2017/4/21.
 */

var commId = "";

function publishNormalCard() {
    if(checkCardState("blacked") < 0){
        return false;
    }

    var cardno = $("#nCardNo").val();
    if (cardno == '') {
        layer.alert("卡号不能为空", {icon: 5});
        return false;
    }

    if ($("#nState").val() == '') {
        layer.alert("卡状态不为空", {icon: 5});
        return false;
    }

    if ($(".permsItem").length == 0) {
        layer.alert("未选择权限!", {icon: 5});
        return false;
    }

    if ($("#selectOwner").text() == '') {
        layer.alert("业主不能为空！", {icon: 5});
        return false;
    }

    var data = "";
    var permItems = $(".permsItem");
    if (permItems.length > 8) {
        layer.alert("目前一张卡只能支持8个门!", {icon: 5});
        return;
    }

    var permStrForCard = "";
    var commCode = $("#sp").val() + $("#commCode").val();
    for (var i = 0; i < permItems.length; i++) {

        var perms = $(".permsItem input")[i].value;
        //console.log("perms " + perms);
        var permsArray = perms.split(":");

        if (commCode != "" && commCode != '' + permsArray[0]) {
            layer.alert("权限只能属于同一个小区!");
            return;
        } else
            commCode = permsArray[0];

        doorNum = permsArray[1];

        permStrForCard += doorNum;

    }

    for (var i = 0; i < (8 - permItems.length); i++) {
        permStrForCard += "0000";
    }

    if (writeCard(commCode, permStrForCard, 6,cardno)) {
        $("#cardType").val("n");
        console.log("permstr " + permStrForCard);

        postJson("/card/save.x",$("#cardSubmit").serialize(),function (data) {
            layer.msg("发卡成功!");
        })
    }

}

var publishSupperCard = function () {
    if(checkCardState("blacked") < 0){
        return false;
    }

    var cardno = $("#sCardNo").val();

    if (cardno == '') {
        layer.alert("卡号不能为空", {icon: 5});
        return false;
    }

    if ($("#sState").val() == '') {
        layer.alert("卡状态不为空", {icon: 5});
        return false;
    }

    var commCode = $("#sp").val() + $("select[name='sCommCode']").val();
    if (writeCard(commCode, "", 10,cardno)) {
        $("#cardType").val("s");
        if ($("select[name='sCommCode']").val() != '') {
            $("#commCode").val($("select[name='sCommCode']").val());
        }

        postJson("/card/save.x",$("#cardSubmit").serialize(),function (data) {
            layer.msg("发卡成功!");
        })
    }

}

function dlgBuildingSelect(owner) {
    if(checkCardState("blacked") < 0){
        return false;
    }

    layer.open({
        type: 2,
        skin: 'layui-layer-demo', //样式类名
        closeBtn: 0, //不显示关闭按钮
        shadeClose: true, //开启遮罩关闭
        fix: false,
        offset: '100px',
        area: ['1000px'],
        title: '楼栋单元选择',
        content: '/BuildingCtrl/selectBuildings.x?Owner=' + owner,
        success: function (layero, index) {
            layer.iframeAuto(index);
        }
    });
}

function dlgAddPerms() {
    if(checkCardState("blacked") < 0){
        return false;
    }

    layer.open({
        type: 2,
        skin: 'layui-layer-demo', //样式类名
        closeBtn: 0, //不显示关闭按钮
        shadeClose: true, //开启遮罩关闭
        fix: false,
        offset: '100px',
        area: ['1000px'],
        title: '楼栋单元选择',
        content: '/BuildingCtrl/selectBuildings.x',
        success: function (layero, index) {
            layer.iframeAuto(index);
        }
    });
}

/* 清除权限窗口 */
function clearPerms() {
    $("#perms").empty();
}
/* 选择刷卡权限单元的 确定按钮的回调接口 */
function callbackDlgAddPerms(commCode, devDisp, devNumber, devAccount) {
    $("#perms").append(
        '<div class="permsItem">' +
        '<div>' +
        '<button type="button" class="permsRemove layui-btn layui-btn-small layui-btn-danger" style="margin-right:20px">删除</button>' +
        '<input type="hidden" name="permsStr" value="'+$("#sp").val() + commCode + ':' + devNumber + ':' + devDisp + ':' + devAccount + '"/>' +
        devDisp +
        '</div>' +
        '<hr>' +
        '</div>'
    );

    $("#perms p").remove();

    $(".permsRemove").click(function () {
        $(this).parent().parent().remove();
    });
}

/* 将普通卡恢复成白卡 */
function resumeNormalCard() {

    var cardNo = $("#nCardNo").val();

    if (cardNo == '') {
        layer.alert("请先读取卡", {icon: 5});
        return false;
    }

    if(checkCardState("supper")<0){
        return false;
    }

    if ($(".permsItem").length == 0) {
        layer.alert("未读取到权限,无法恢复", {icon: 5});
        return false;
    }

    var commCode = $("#commCode").val();
    if(commCode == null || commCode.length !=4){
        layer.alert("未读取到数据，无法恢复");
        return false;
    }

    var sKey = $("#sp").val() + $("#commCode").val();

    var ret = resumeCard(sKey);
    if(ret != 0){
        if(ret == 110)
            layer.msg("白卡，不需要恢复!");
        else if(ret == 111)
            layer.msg("秘钥无法识别，恢复失败!");
        else
            layer.alert("恢复失败！");
        return false;
    }


    var param = {cardNo : cardNo};
    postJson("/card/resumeCard.x",param,function () {
        layer.alert("卡恢复成功!");
    })

}

/* 将超级卡恢复成白卡 */
function resumeSupperCard() {
    var cardNo = $("#sCardNo").val();

    if (cardNo == '') {
        layer.alert("请先读取卡", {icon: 5});
        return false;
    }

    if(checkCardState("normal")<0){
        return false;
    }

    var commCode = $("select[name='sCommCode']").val();
    if(commCode == null || commCode.length !=4){
        layer.alert("未读取到数据，无法恢复");
        return false;
    }

    var sKey = $("#sp").val() + $("#commCode").val();

    var ret = resumeCard(sKey);
    if(ret != 0){
        if(ret == 110)
            layer.msg("白卡，不需要恢复!");
        else if(ret == 111)
            layer.msg("秘钥无法识别，恢复失败!");
        else
            layer.alert("恢复失败！");
        return false;
    }


    var param = {cardNo : cardNo};
    postJson("/card/resumeCard.x",param,function () {
        layer.alert("卡恢复成功!");
    })
}